﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MusicPlayer.Properties;
using WMPLib;

namespace MusicPlayer
{
    public partial class PlayForm : Form
    {
        Point offset;       //鼠标与窗口的距离
        ImageList StopImages;
        ImageList PrevImages;
        ImageList NextImages;
        ImageList PlayImages;
        ImageList PauseImages;
        ImageList OpenImages;
        ImageList MuteImages;
        ImageList ThumbImages;
        ImageList ListImages;
        Image ProgressImage;
        Image ProgressVolumeImage;

        static Music currentSong;
        static IWMPMedia currentMedia;
        static ArrayList currentList = new ArrayList();
        static IWMPPlaylist currentPlaylist;
        WindowsMediaPlayerClass player = new WindowsMediaPlayerClass();
        PlayList playList = new PlayList(ref currentSong,ref currentList);
        WMPPlayState state;
        int currentNumber;
        bool isLeftTime=false ;
        
        public PlayForm()
        {
            InitializeComponent();

            buttonClose.ImageList = DivideImage(Resources.close, 17, 15, 4);
            buttonClose.ImageIndex = 0;
            buttonMin.ImageList = DivideImage(Resources.minimize, 17, 15, 4);
            buttonMin.ImageIndex = 0;

            StopImages  = DivideImage(Resources.stop, 35, 35, 4);
            PrevImages  = DivideImage(Resources.prev, 35, 35, 4);
            NextImages  = DivideImage(Resources.next ,35, 35, 4);
            PlayImages  = DivideImage(Resources.play, 50, 50, 4);
            PauseImages = DivideImage(Resources.pause, 50, 50, 4);
            OpenImages = DivideImage(Resources.open, 35, 35, 4);
            MuteImages = DivideImage(Resources.mute, 17, 14, 4);
            ThumbImages = DivideImage(Resources.thumb, 11, 11, 4);
            ListImages = DivideImage(Resources.list, 64, 21, 4);
            ProgressImage = Resources.progress_fill;
            ProgressVolumeImage = Resources.progress2;

            pictureBoxStop.Image = StopImages.Images[0];
            pictureBoxPrev.Image = PrevImages.Images[3];
            pictureBoxNext.Image = NextImages.Images[0];
            pictureBoxPlay.Image = PlayImages.Images[0];
            pictureBoxOpen.Image = OpenImages.Images[0];
            pictureBoxMute.Image = MuteImages.Images[0];
            pictureBoxThumb.Image  = ThumbImages.Images[0];
            pictureBoxList.Image = ListImages.Images[1];
            pictureBoxProgressVolume.Image = ProgressVolumeImage;
            pictureBoxProgress.Image  = ProgressImage;
            pictureBoxIcon.Image =Resources.TTPlayer.ToBitmap();
            pictureBoxIcon.SizeMode = PictureBoxSizeMode.StretchImage ;
            pictureBoxIcon.BackgroundImage = SubImage(Resources.player_skin, new Point(6, 5), new Size(16, 16));
            panel1.BackgroundImage=SubImage(Resources.player_skin,new Point(8,25),new Size(294,24));
            panel2.BackgroundImage = SubImage(Resources.player_skin, new Point(207, 77), new Size(90, 10));

            currentList.Add(new Music("千里之外", @"E:\Music\周杰伦\千里之外.wma"));
            currentList.Add(new Music("发如雪", @"E:\Music\周杰伦\发如雪.mp3"));
            currentSong =(Music ) currentList[0];
            currentNumber = 0;
            
            player.currentPlaylist.appendItem 
            player.playState = WMPPlayState.wmppsStopped;
            player.

        }

        private void PlayForm_Load(object sender, EventArgs e)
        {
            playList.Owner = this;
            playList.Show();
            playList.Location = new Point(this.Left, this.Bottom);
        } 

        public static ImageList DivideImage(Image source, int width, int height, int number)
        {
            ImageList result=new ImageList();
            result.ImageSize = new Size(width, height);
            result.ColorDepth = ColorDepth.Depth24Bit;
            for (int i = 0; i < number; i++)
            {
                Image image=new Bitmap(width,height);
                Graphics g=Graphics.FromImage(image);
                g.DrawImage(source , new Rectangle(new Point(0,0),new Size(width,height)),new Rectangle(new Point(i*width,0),new Size(width,height)),GraphicsUnit.Pixel);
                result.Images.Add(image);
                //image.Dispose();
                //g.Dispose();
            }
            return result;
        }

        public static Image SubImage(Image source, Point start,Size  region)
        {
            Image result = new Bitmap(region.Width, region.Height);
            Graphics g = Graphics.FromImage(result );
            g.DrawImage(source, new Rectangle(new Point(0, 0), region), new Rectangle(start ,region), GraphicsUnit.Pixel);
            g.Dispose();
            return result;
        }
        //
        //主窗口事件
        //
        private void pictureBoxForm_MouseDown(object sender, MouseEventArgs e)
        {
            if (MouseButtons.Left != e.Button) return;
            
            Point cur = this.PointToScreen(e.Location);
            offset = new Point(cur.X - this.Left, cur.Y - this.Top);
        }

        private void pictureBoxForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (MouseButtons.Left != e.Button) return;
            Point cur = MousePosition;
            this.Location = new Point(cur.X - offset.X, cur.Y - offset.Y);
        }
        //
        //关闭按钮
        //
        private void buttonClose_MouseEnter(object sender, EventArgs e)
        {
            buttonClose.ImageIndex = 1;
            this.Cursor = Cursors.Hand;
            toolTip.Show("关闭窗口", buttonClose, 20, 20);
        }

        private void buttonClose_MouseLeave(object sender, EventArgs e)
        {
            buttonClose.ImageIndex = 0;
            this.Cursor = Cursors.Default ;
            toolTip.Hide(buttonClose);
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            buttonClose.ImageIndex = 2;
            Application.Exit();
        }
        //
        //最小化按钮
        //
        private void buttonMin_MouseEnter(object sender, EventArgs e)
        {
            buttonMin.ImageIndex = 1;
            this.Cursor = Cursors.Hand;
            toolTip.Show("最小化窗口", buttonMin,20,20);
        }

        private void buttonMin_MouseLeave(object sender, EventArgs e)
        {
            buttonMin.ImageIndex = 0;
            this.Cursor = Cursors.Default ;
            toolTip.Hide(buttonMin);
        }

        private void buttonMin_Click(object sender, EventArgs e)
        {
            buttonMin.ImageIndex = 2;
            this.WindowState = FormWindowState.Minimized;
        }
        //
        //停止按钮
        //
        private void pictureBoxStop_MouseEnter(object sender, EventArgs e)
        {
            if (player.playState != WMPPlayState.wmppsStopped)
            {
                pictureBoxStop.Image = StopImages.Images[1];
                this.Cursor = Cursors.Hand;
            }
        }

        private void pictureBoxStop_MouseDown(object sender, MouseEventArgs e)
        {
            if (player.playState != WMPPlayState.wmppsStopped)
            {
                pictureBoxStop.Image = StopImages.Images[2];
            }
        }

        private void pictureBoxStop_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
            if (player.playState != WMPPlayState.wmppsStopped)
            {
                pictureBoxStop.Image = StopImages.Images[0];                
            }
        }

        private void pictureBoxStop_Click(object sender, EventArgs e)
        {
            pictureBoxStop.Image = StopImages.Images[3];
            pictureBoxPlay.Image = PlayImages.Images[0];
            player.stop();
        }
        //
        //前一首按钮
        //
        private void pictureBoxPrev_MouseEnter(object sender, EventArgs e)
        {
            if (currentNumber != 0)
            {
                pictureBoxPrev.Image = PrevImages.Images[1];
                this.Cursor = Cursors.Hand;
            }
        }

        private void pictureBoxPrev_MouseDown(object sender, MouseEventArgs e)
        {
            if (currentNumber != 0)
            {
                pictureBoxPrev.Image = PrevImages.Images[2];
            }
        }

        private void pictureBoxPrev_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
            if (currentNumber != 0) pictureBoxPrev.Image = PrevImages.Images[0];
        }

        private void pictureBoxPrev_Click(object sender, EventArgs e)
        {
            if (currentNumber != 0)
            {                
                player.previous();
                currentNumber--;
            }
            if(currentNumber == 0)pictureBoxPrev.Image = PrevImages.Images[3];
        }
        //
        //下一首按钮
        //
        private void pictureBoxNext_MouseEnter(object sender, EventArgs e)
        {
            if (currentNumber < player.currentPlaylist.count)
            {
                pictureBoxNext.Image = NextImages.Images[1];
                this.Cursor = Cursors.Hand;
            }
        }
        private void pictureBoxNext_MouseDown(object sender, MouseEventArgs e)
        {
            if (currentNumber < player.currentPlaylist.count)
            {
                pictureBoxNext.Image = NextImages.Images[2];
            }
        }
        private void pictureBoxNext_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
            if (currentNumber < player.currentPlaylist.count) pictureBoxNext.Image = NextImages.Images[0];
        }

        private void pictureBoxNext_Click(object sender, EventArgs e)
        {
            if (currentNumber < player.currentPlaylist.count)
            {               
                player.next();
                currentNumber++;
                if (currentNumber == 1) pictureBoxPrev.Image = PrevImages.Images[0];
            } 
            if(currentNumber == player.currentPlaylist.count) pictureBoxNext.Image = NextImages.Images[3];
        }
        //
        //播放按钮
        //
        private void pictureBoxPlay_MouseEnter(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
            if(state==WMPPlayState.wmppsStopped ||state == WMPPlayState.wmppsPaused ) pictureBoxPlay.Image = PlayImages.Images[1];
            if (state == WMPPlayState.wmppsPlaying ) pictureBoxPlay.Image = PauseImages.Images[1];
        }

        private void pictureBoxPlay_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
            if (state == WMPPlayState.wmppsStopped || state == WMPPlayState.wmppsPaused) pictureBoxPlay.Image = PlayImages.Images[0];
            if (state == WMPPlayState.wmppsPlaying) pictureBoxPlay.Image = PauseImages.Images[0];
        }

        private void pictureBoxPlay_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
            if (state == WMPPlayState.wmppsStopped)
            {
                pictureBoxStop.Image = StopImages.Images[0];
                pictureBoxPlay.Image = PauseImages.Images[0];
                state = WMPPlayState.wmppsPlaying;
                player.play();
 
            }
            if (state == WMPPlayState.wmppsPaused)
            {
                pictureBoxPlay.Image = PauseImages.Images[0];
                state = WMPPlayState.wmppsPlaying;

            }
            if (state == WMPPlayState.wmppsPlaying)
            {
                pictureBoxPlay.Image = PlayImages.Images[0];
                state = WMPPlayState.wmppsPaused;
                player.resume();
            }            
            
        }
        //
        //静音按钮
        //
        private void pictureBoxMute_MouseEnter(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
            if (player.mute == false) pictureBoxMute.Image = MuteImages.Images[1];
        }

        private void pictureBoxMute_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
            if (player.mute == false) pictureBoxMute.Image = MuteImages.Images[0];
        }

        private void pictureBoxMute_Click(object sender, EventArgs e)
        {
            if (player.mute == false )
            {
                pictureBoxMute.Image = MuteImages.Images[2];
                player.mute = true;
            }
            else
            {
                pictureBoxMute.Image = MuteImages.Images[0];
                player.mute = false;
            }
        }
        //
        //控制音量大小
        //
        private void panel2_MouseEnter(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
        }

        private void panel2_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
        }

        private void panel2_MouseClick(object sender, MouseEventArgs e)
        {
            //player.volume = (e.X - 207) * 100 / 90;
            player.settings.volume=(e.X - 207) * 100 / 90;
            pictureBoxProgressVolume.Size = new Size(e.X, 9);
        }

        private void pictureBoxProgressVolume_MouseEnter(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
        }

        private void pictureBoxProgressVolume_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
        }

        private void pictureBoxProgressVolume_MouseClick(object sender, MouseEventArgs e)
        {
            player.settings .volume = (e.X - 207) * 100 / 90;
            pictureBoxProgressVolume.Size = new Size(e.X, 9);
        }
        //
        //打开按钮
        //
        private void pictureBoxOpen_Click(object sender, EventArgs e)
        {
            pictureBoxOpen.Image=OpenImages.Images[2];            
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (openFileDialog1.FileNames.Length == 0) return;
                int start=openFileDialog1.FileNames[0].LastIndexOf('\\');
                int end = openFileDialog1.FileNames[0].LastIndexOf('.');
                currentSong = new Music(openFileDialog1.FileNames[0].Substring(start + 1, end - start), openFileDialog1.FileNames[0]);
                for (int i = 0; i < openFileDialog1.FileNames.Length; i++)
                {
                    start=openFileDialog1.FileNames[i].LastIndexOf('\\');
                    end=openFileDialog1.FileNames[i].LastIndexOf('.');
                    Music tem=new Music(openFileDialog1.FileNames[i].Substring(start+1,end-start),openFileDialog1.FileNames[i]);
                    currentList.Add(tem);
                }
            }           

        }

        private void pictureBoxOpen_MouseEnter(object sender, EventArgs e)
        {
            pictureBoxOpen.Image = OpenImages.Images[1];
            this.Cursor = Cursors.Hand;
        }

        private void pictureBoxOpen_MouseLeave(object sender, EventArgs e)
        {
            pictureBoxOpen.Image = OpenImages.Images[0];
            this.Cursor = Cursors.Default ;
        }

        private void pictureBoxOpen_MouseDown(object sender, MouseEventArgs e)
        {
            pictureBoxOpen.Image = OpenImages.Images[2];
        }
        //     
        //时间和进度条显示
        //
        private void timer1_Tick(object sender, EventArgs e)
        {
            pictureBoxThumb.Left = 13 + (int)(player.currentPosition * 282 / player.currentItem.duration);

            if (state == WMPPlayState.wmppsStopped || state == WMPPlayState.wmppsUndefined)
            {
                labelTime.Text = "00:00";
                return;
            }
            if (isLeftTime == false)
            {
                string minute = Convert.ToString((int)player.currentPosition / 60);
                if (minute.Length < 2) minute = '0' + minute;
                string second = Convert.ToString((int)player.currentPosition % 60);
                if (second.Length == 1) second = '0' + second;
                labelTime.Text = string.Format("{0}:{1}", minute, second);
                labelTime.Left = 243;
            }
            else
            {
                string minute = Convert.ToString((int)(player.currentItem.duration- player.currentPosition) / 60);
                if (minute.Length < 2) minute = '0' + minute;
                string second = Convert.ToString((int)(player.currentItem.duration - player.currentPosition) % 60);
                if (second.Length == 1) second = '0' + second;
                labelTime.Text = string.Format("━ {0}:{1}", minute, second);
                labelTime.Left =232;
            }
            
            labelName.Text = player.currentItem.name;
        }

        private void labelTime_MouseClick(object sender, MouseEventArgs e)
        {
            isLeftTime = !isLeftTime;
        }
        //
        //打开播放列表
        //
        private void pictureBoxList_MouseEnter(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
            if (playList.Visible == false) pictureBoxList.Image = ListImages.Images[1];
        }

        private void pictureBoxList_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
            if (playList.Visible == false) pictureBoxList.Image = ListImages.Images[0];
        }

        private void pictureBoxList_MouseClick(object sender, MouseEventArgs e)
        {
            if (playList.Visible == false)
            {
                playList.Visible = true;
                pictureBoxList.Image = ListImages.Images[2];
            }
            else
            {
                playList.Visible = false;
                pictureBoxList.Image = ListImages.Images[0];
            }
        }
        //
        //标题栏图标
        //
        private void pictureBoxIcon_Click(object sender, EventArgs e)
        {
            contextMenuStrip1.Show(pictureBoxIcon, new Point(5, 18));
        }

        private void pictureBoxIcon_MouseEnter(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
        }

        private void pictureBoxIcon_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default ;
        }

                                    
    }
}
